﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TestingIoC.ServiceReference;
using System.Collections.Generic;
using TestingIoC.Helpers;
using Ninject;

namespace TestingIoC.ViewModel
{
    public class SlowStringViewModel: ViewModelBase
    {
        private Func<TestingIocServiceClient> proxyFactory;
        private TestingIocServiceClient lastCreatedProxy;
        
        [Inject]
        public SlowStringViewModel(Func<TestingIocServiceClient> proxyFactory)
        {
            this.proxyFactory = proxyFactory;
                        
            this.GetNewString = new RelayCommand(GetNewStringPressed);

            this.tempString = new List<string>();
        }

        void proxy_GetSlowStringCompleted(object sender, GetSlowStringCompletedEventArgs e)
        {
            this.lastCreatedProxy.GetSlowStringCompleted -= proxy_GetSlowStringCompleted;
            this.tempString.Add(e.Result);
            this.SlowStrings = new List<string>();
            this.SlowStrings = tempString;
        }

        List<string> tempString;
        private List<string> slowStrings;

        public List<string> SlowStrings
        {
            get { return slowStrings; }
            set
            {                
                if (slowStrings != value)
                {
                    slowStrings = value;
                    RaisePropertyChanged("SlowStrings");
                }                
            }
        }

        public RelayCommand GetNewString { get; set; }

        private void GetNewStringPressed()
        {
            this.lastCreatedProxy = this.proxyFactory();
            this.lastCreatedProxy.GetSlowStringCompleted += proxy_GetSlowStringCompleted;
            this.lastCreatedProxy.GetSlowStringAsync();
        }

    }
}
